﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Expenses.Models;
using Expenses.ViewModels;

namespace Expenses.Controllers.API
{
    public class ExpensesController : ApiController
    {
        private ExpensesContext db = new ExpensesContext();

        // GET api/Expenses
        public ExpensesHome GetExpenseRecords()
        {
            var expensesList = db.ExpenseRecords.Where(p => p.User.UserName == User.Identity.Name).AsEnumerable();
            var titles = db.ExpenseRecords.Where(p => p.User.UserName == User.Identity.Name).GroupBy(p => p.Title).Select(p => new TitleRecap { Name = p.Key, Cost = p.Sum(c => c.Cost) }).OrderByDescending(p =>p.Cost).ToList();
            
            return new ExpensesHome
            {
                Expenses = expensesList,
                Titles = titles
            };
        }

        // GET api/Expenses/5
        public ExpenseRecord GetExpenseRecord(int id)
        {
            ExpenseRecord expenserecord = db.ExpenseRecords.Find(id);
            if (expenserecord == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return expenserecord;
        }

        // PUT api/Expenses/5
        public HttpResponseMessage PutExpenseRecord(int id, ExpenseRecord expenserecord)
        {
            if (ModelState.IsValid && id == expenserecord.ExpenseRecordId)
            {
                db.Entry(expenserecord).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Expenses
        public HttpResponseMessage PostExpenseRecord(ExpenseRecord expenserecord)
        {
            if (ModelState.IsValid)
            {
                //expenserecord.Date = DateTime.UtcNow;
                expenserecord.User = db.UserProfiles.Single(p => p.UserName == User.Identity.Name);

                db.ExpenseRecords.Add(expenserecord);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, expenserecord);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = expenserecord.ExpenseRecordId }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Expenses/5
        public HttpResponseMessage DeleteExpenseRecord(int id)
        {
            ExpenseRecord expenserecord = db.ExpenseRecords.Find(id);
            if (expenserecord == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.ExpenseRecords.Remove(expenserecord);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, expenserecord);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}